<?php

declare(strict_types=1);
/**
 * This file is part of MineAdmin.
 *
 * @link     https://www.mineadmin.com
 * @document https://doc.mineadmin.com
 * @contact  root@imoi.cn
 * @license  https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
 */

namespace App\Schema\Question;

use App\Model\Question;
use App\Model\Permission\User;
use Hyperf\Swagger\Annotation\Property;
use Hyperf\Swagger\Annotation\Schema;

#[Schema]
final class QuestionSchema implements \JsonSerializable
{
    public ?string $remark;

    public function __construct(
        Question $model,
        #[Property(property: 'title', title: '题干', type: 'string')]
        public ?string $title,
        #[Property(property: 'group', title: '分组', type: 'string')]
        public ?string $group,
        #[Property(property: 'group_id', title: '分组ID', type: 'string')]
        public ?string $group_id,
        #[Property(property: 'level', title: '难度', type: 'string')]
        public ?string $level,
        #[Property(property: 'answers', title: '答案', type: 'array')]
        public ?array $answers,
        #[Property(property: 'analysis', title: '解析', type: 'string')]
        public ?string $analysis,
        #[Property(property: 'type', title: '题型', type: 'string')]
        public ?string $type,
        #[Property(property: 'creator_id', title: '创建者', type: 'int')]
        public ?int $creator_id,
        #[Property(property: 'creator', title: '创建者', type: 'array')]
        public ?User $creator,
    ) {

        foreach (
            get_class_vars(__CLASS__)
            as $var
        ) {
            $this->{$var} = $model->${$var};
        }
    }

    public function jsonSerialize(): mixed
    {
        $json = [];
        foreach (
            get_class_vars(__CLASS__)
            as $var
        ) {
            $json[$var] = $this->{$var};
        }
        return $json;
    }
}
